package com.biu.billiards.task;
import com.biu.billiards.model.entity.Hardware;
import com.biu.billiards.model.vo.mqtt.OpenDeviceVo;
import com.biu.billiards.service.IDeviceService;
import com.biu.billiards.service.IHardwareService;
import com.biu.billiards.service.IOrderService;
import com.biu.billiards.service.IShopService;
import com.biu.billiards.utils.RedisUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName AsyncTask
 * @Description TODO
 * @Author zrc
 * @Date 15:43
 * @Version 1.0
 **/
@Component
public class AsyncTask {


    @Resource
    private RedisUtils redisUtils;
    @Resource
    private IDeviceService deviceService;

    @Resource
    private IShopService shopService;


    @Resource
    private IOrderService orderService;


    @Resource
    private IHardwareService hardwareService;






    //模拟异步方法1
    @Async
    public Boolean openDoor(OpenDeviceVo openDeviceVo) throws InterruptedException {

        System.out.println("openDoor");
        openDeviceVo.setOpen(0);
        deviceService.openDoor(openDeviceVo);
        Thread.sleep(20000);
        openDeviceVo.setOpen(1);
        deviceService.openDoor(openDeviceVo);

        return true;

    }



    @Async
    public Boolean autoDevice(String topic) throws InterruptedException {

        Hardware hardware = hardwareService.getHardware(topic);

        System.out.println("openDooropenDooropenDooropenDoor");
        System.out.println(topic);
        System.out.println(hardware);

        if (hardware!=null){

            if (shopService.getAutoStatus(topic)){

                System.out.println("自动化打开的");

                openDevice(topic,hardware.getShopId());

            }

        }

        return true;

    }

        private void openDevice(String receivedTopic,Integer shopId) {

            Boolean open = shopService.getAutoOpenStatus(receivedTopic);

            System.out.println("open");

            if (open) {

                redisUtils.set(receivedTopic, "1", 5, TimeUnit.MINUTES);
                System.out.println("有人在玩");
                deviceService.openVrv(open,shopId);


            } else {

                System.out.println("没人在玩");
                if (redisUtils.get(receivedTopic) == null) {
                    System.out.println("真正关闭");
                    deviceService.openVrv(open,shopId);
                }
            }

        }



//    //模拟异步方法2
//    @Async
//    public Future<List<Product>> asyncGetData2() throws InterruptedException {
//        System.out.println("开始向数据库查询product集合");
//        List<Product> productList = new ArrayList<>();
//        Product product = new Product();
//        product.setProductName("产品1");
//        productList.add(product);
//        Thread.sleep(3000);
//        System.out.println("结束向数据库查询product集合");
//        return new AsyncResult<>(productList);
//    }

}